package com.ft.admix.bd.adapter;

import android.app.Activity;

import androidx.annotation.Nullable;

import com.ft.admix.ad.MixCustomInterstitialAdapter;
import com.ft.admix.config.AdConfig;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsInterstitialAd;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsVideoPlayConfig;

import java.util.List;

public class InterstitialAdLoader extends MixCustomInterstitialAdapter {
    private KsInterstitialAd mKsInterstitialAd;

    @Override
    protected void loadNetworkAd(Activity context, AdConfig config) {
        mKsInterstitialAd = null;
        KsScene scene = new KsScene.Builder(Long.parseLong(config.getAdId())).build();
        KsAdSDK.getLoadManager().loadInterstitialAd(scene,
                new KsLoadManager.InterstitialAdListener() {
                    @Override
                    public void onError(int i, String s) {
                        callLoadFailed(i, s);
                    }

                    @Override
                    public void onRequestResult(int adNumber) {
//                        ToastUtil.showToast(mContext, "插屏广告请求填充个数 " + adNumber);
                    }

                    @Override
                    public void onInterstitialAdLoad(@Nullable List<KsInterstitialAd> adList) {
                        if (adList != null && adList.size() > 0) {
                            mKsInterstitialAd = adList.get(0);
                            mKsInterstitialAd.setAdInteractionListener(new KsInterstitialAd.AdInteractionListener() {
                                @Override
                                public void onAdClicked() {
                                    callInterstitialAdClick();
                                }

                                @Override
                                public void onAdShow() {
                                    callInterstitialAdShow();
                                }

                                @Override
                                public void onAdClosed() {
                                    callInterstitialAdClose();
                                }

                                @Override
                                public void onPageDismiss() {
                                }

                                @Override
                                public void onVideoPlayError(int i, int i1) {
                                    callInterstitialVideoError();
                                }

                                @Override
                                public void onVideoPlayEnd() {
                                    callInterstitialVideoComplete();
                                }

                                @Override
                                public void onVideoPlayStart() {
                                    callInterstitialVideoStart();
                                }

                                @Override
                                public void onSkippedAd() {
                                    callInterstitialAdSkip();
                                }
                            });
                            callLoadSuccess(mKsInterstitialAd.getECPM());
                        }
                    }
                });
    }

    @Override
    public void show(Activity activity) {
        if (this.mKsInterstitialAd != null) {
            KsVideoPlayConfig videoPlayConfig = new KsVideoPlayConfig.Builder()
                    .build();
            this.mKsInterstitialAd.showInterstitialAd(activity, videoPlayConfig);
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        if (mKsInterstitialAd != null) {
        }
    }
}
